﻿using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using UnityEngine;
using UnityEngine.Networking;

public class Warp : Entity, IEditable
{
    [CompilerGenerated]
    private static Predicate<Setting> <>f__am$cache6;
    [CompilerGenerated]
    private static Func<Setting, float> <>f__am$cache7;
    public string destLevelName;
    private HashSet<PlayerCharacter> enteredPlayers = new HashSet<PlayerCharacter>();
    private static float lastWarpTime;
    public bool randomDest;
    public bool resetPlayer;
    public List<Setting> settings = new List<Setting>();

    [CompilerGenerated]
    private void <OnEditorGUI>m__14B(int i)
    {
        if (this.settings[i] == null)
        {
            this.settings[i] = new Setting();
        }
        GUILayout.BeginHorizontal(new GUILayoutOption[0]);
        this.settings[i].destLevelName = GameGUI.AssetNameField(this.settings[i].destLevelName);
        this.settings[i].chance = GameGUI.FloatField(this.settings[i].chance, 0.1f);
        GUILayout.EndHorizontal();
    }

    void IEditable.OnEditorGUI()
    {
        this.resetPlayer = GameGUI.BoolField("重置玩家", this.resetPlayer);
        this.randomDest = GameGUI.BoolField("随机目的地", this.randomDest);
        if (this.randomDest)
        {
            GameGUI.ListField<Setting>("目的地列表（关卡名/机会）", this.settings, new Action<int>(this.<OnEditorGUI>m__14B));
        }
        else
        {
            if (this.settings.Count == 0)
            {
                this.settings.Add(new Setting());
            }
            this.settings[0].destLevelName = GameGUI.AssetNameField("目的地关卡名", this.settings[0].destLevelName);
        }
    }

    public override void OnDeserialize(NetworkReader reader, bool initialState)
    {
        base.OnDeserialize(reader, initialState);
    }

    public override bool OnSerialize(NetworkWriter writer, bool forceAll)
    {
        bool flag;
        bool flag2 = base.OnSerialize(writer, forceAll);
        return (flag | flag2);
    }

    private void OnTriggerEnter(Collider co)
    {
        if ((((Time.time - lastWarpTime) >= 0.5f) && NetworkUtility.isServer) && !co.isTrigger)
        {
            PlayerCharacter item = co.GetCharacter() as PlayerCharacter;
            if (item != null)
            {
                this.enteredPlayers.Add(item);
            }
            if (this.enteredPlayers.Count == PlayerCharacter.all.Count)
            {
                <OnTriggerEnter>c__AnonStorey7A storeya = new <OnTriggerEnter>c__AnonStorey7A {
                    <>f__this = this
                };
                if (<>f__am$cache6 == null)
                {
                    <>f__am$cache6 = x => !AssetManager.HasAsset(x.destLevelName);
                }
                this.settings.RemoveAll(<>f__am$cache6);
                if (<>f__am$cache7 == null)
                {
                    <>f__am$cache7 = x => x.chance;
                }
                storeya.setting = this.settings.GetRandomElementByWeight<Setting>(<>f__am$cache7);
                if (storeya.setting != null)
                {
                    lastWarpTime = Time.time;
                    ScreenFader.Fade(new Func<YieldInstruction>(storeya.<>m__14E));
                }
            }
        }
    }

    private void OnTriggerExit(Collider co)
    {
        if (NetworkUtility.isServer && !co.isTrigger)
        {
            PlayerCharacter item = co.GetCharacter() as PlayerCharacter;
            if (item != null)
            {
                this.enteredPlayers.Remove(item);
            }
        }
    }

    private void UNetVersion()
    {
    }

    [CompilerGenerated]
    private sealed class <OnTriggerEnter>c__AnonStorey7A
    {
        internal Warp <>f__this;
        internal Warp.Setting setting;

        internal YieldInstruction <>m__14E()
        {
            if (this.<>f__this.resetPlayer)
            {
                return NetworkUtility.LoadLevel(this.setting.destLevelName);
            }
            return NetworkUtility.Warp(this.setting.destLevelName);
        }
    }

    [Serializable]
    public class Setting
    {
        public float chance = 1f;
        public string destLevelName;
    }
}

